package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.LabelParam;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.LabelParamDataHandler;
import com.efast.dmom.mes.entity.LabelParamEntity;
import com.efast.dmom.mes.service.LabelParamService;

@Service
public class LabelParamServiceImpl implements LabelParamService {
	
	@Autowired
	private LabelParamDataHandler labelParamDataHandler;

	@Override
	public ResultBean save(LabelParam labelParam) {
		ResultBean resultBean = new ResultBean(true,' ');
		LabelParamEntity labelParamEntity = labelParam.getInnerData();
		if (labelParamEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的信息为空！");
			return resultBean;
		}
		if (labelParamEntity.getLabelId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请选择一条标签数据进行新增");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelParamEntity.getLabelParamCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签参数编码是否填写！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelParamEntity.getLabelParamValue())){
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签参数值是否填写！");
			return resultBean;
		}
		LabelParam labelParamByLabelId = labelParamDataHandler.getByLabelIdAndParam(labelParam);
		if (labelParamByLabelId.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("同一标签下的标签参数编码不能重复！");
			return resultBean;
		}
		return labelParamDataHandler.save(labelParam);
	}

	@Override
	public ResultBean update(LabelParam labelParam) {
		ResultBean resultBean = new ResultBean(true,' ');
		LabelParamEntity labelParamEntity = labelParam.getInnerData();
		if (labelParamEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的信息为空！");
			return resultBean;
		}
		if (labelParamEntity.getLabelId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请选择一条标签数据！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelParamEntity.getLabelParamCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签参数编码是否填写！");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(labelParamEntity.getLabelParamValue())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查标签参数值是否填写！");
			return resultBean;
		}
		if (labelParamEntity.getIsEdit() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该条数据不可编辑！");
			return resultBean;
		}
		LabelParam labelParamById = labelParamDataHandler.getById(labelParamEntity.getId());
		if (!labelParamEntity.getLabelParamCode().equals(labelParamById.getInnerData().getLabelParamCode())) {
			LabelParam labelParamByLabelId = labelParamDataHandler.getByLabelIdAndParam(labelParam);
			if (labelParamByLabelId.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("同一标签下的标签参数编码不能重复！");
				return resultBean;
			}
		}
		return labelParamDataHandler.update(labelParam);
	}

	@Override
	public ResultBean delete(LabelParam labelParam) {
		ResultBean resultBean = new ResultBean(true,' ');
		LabelParamEntity labelParamEntity = labelParam.getInnerData();
		if (labelParamEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请选择一条数据进行删除！");
			return resultBean;
		}
		return labelParamDataHandler.delete(labelParam);
	}

	@Override
	public ResultBean deleteBatch(List<LabelParam> labelParamList) {
		ResultBean resultBean = new ResultBean(true,' ');
		if (labelParamList == null || labelParamList.size() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请至少选择一条数据进行删除！");
			return resultBean;
		}
		return labelParamDataHandler.deleteBatch(labelParamList);
	}
	
	

}
